/*******************************************************************************
 * Copyright (c) 2011 GigaSpaces Technologies Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

package org.cloudifysource.dsl.internal.packaging;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;

/**
 * 
 * @author elip
 *
 */
public class FileAppender {
	
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");
	
	private FileWriter writer;
	private File finalFile;

	public FileAppender(final String fileName) 
			throws IOException {
		createAppenderFile(File.createTempFile(fileName, ""));
	}
	
	public FileAppender(final File newFile) throws IOException {
		createAppenderFile(newFile);
	}
	
	private void createAppenderFile(final File newFile) throws IOException {
		if (newFile.exists()) {
			String fileContents = FileUtils.readFileToString(newFile);
			writer = new FileWriter(newFile);
			writer.append(fileContents);
			writer.append(LINE_SEPARATOR);
		} else {
			writer = new FileWriter(newFile);
		}	
		finalFile = newFile;
	}
	
	/**
	 * @param comment - this comment will be appended to the beginning of the file to merge.
	 * @param fileToMerge - the file to merge with
	 * @return .
	 * @throws IOException 
	 */
	public FileAppender append(final String comment, final File fileToMerge) throws IOException {
		return append(comment, FileUtils.readFileToString(fileToMerge));
	}
	
	/**
	 * @param comment - this comment will be appended to the beginning of the file to merge.
	 * @param contentToMerge - the content to merge with
	 * @return .
	 * @throws IOException 
	 */
	public FileAppender append(final String comment, final String contentToMerge) throws IOException {
		writer.append("//").append(comment).append(" Generated By ").append(FileAppender.class.getName())
            .append(" On : ").append(new Date(System.currentTimeMillis()).toString())
		    .append(LINE_SEPARATOR)
		    .append(LINE_SEPARATOR)
		    .append(contentToMerge)
		    .append(LINE_SEPARATOR)
		    .append(LINE_SEPARATOR);
		return this;
	}
	
	/**
	 * Append all the files and copy the result file to dest file.
	 * @param dest the destination file will contain all the appended files.
	 * @param filesToAppend A map of files and comments to append.
	 * @throws IOException .
	 */
	public void appendAll(final File dest, final LinkedHashMap<File, String> filesToAppend) 
			throws IOException {
		for (Entry<File, String> fileEntry : filesToAppend.entrySet()) {
			final File file = fileEntry.getKey();
			if (file != null && file.exists()) {
				append(fileEntry.getValue(), file);
			}
		}
		flush();
		FileUtils.copyFile(finalFile, dest);

		finalFile.delete();
	}
	
	/**
	 * 
	 * @throws IOException .
	 */
	public void flush() throws IOException {
		writer.flush();
		writer.close();
	}
}
